package ca.seia.comp6521.a2.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import ca.seia.comp6521.a2.itemsetstorage.CharTriplet;

public class ObjectSerializer {
	
	private static String FILEPATH_STORAGE = "c:\\Temp";
	private static String FILENAME = "serialTriplets";
	
	private List<String> filePathsToSerializedCandidates;
	public int countNbrTimesSerializedToDisk;
	
	public ObjectSerializer() {
		this.filePathsToSerializedCandidates = new ArrayList<>();
		this.countNbrTimesSerializedToDisk = 0;
	}
	
	
	
	public void serializeToDisk(List<CharTriplet> candidateTriplets) throws IOException {
		String filePath = FILEPATH_STORAGE+"\\"+FILENAME+countNbrTimesSerializedToDisk+".txt";
		FileOutputStream fileOut =  new FileOutputStream(filePath);
        ObjectOutputStream out =  new ObjectOutputStream(fileOut);
        out.writeObject(candidateTriplets);
        out.close();
        this.filePathsToSerializedCandidates.add(filePath);
        countNbrTimesSerializedToDisk++;
	}
	
	@SuppressWarnings("unchecked")
	public List<CharTriplet> deserializeFromDisk(int fileNumber) throws Exception {
		String filePath = this.filePathsToSerializedCandidates.get(fileNumber);
		FileInputStream fis =new FileInputStream(filePath);
        ObjectInputStream ois = new ObjectInputStream(fis);
        List<CharTriplet> charTriplets =  (ArrayList<CharTriplet>) ois.readObject();
        ois.close();
        return charTriplets;
	}
	
	
	public static void main(String[] args) throws Exception {
		ObjectSerializer serial = new ObjectSerializer();
		CharTriplet c = new CharTriplet(3, 88, 100);
		CharTriplet c2 = new CharTriplet(100, 900, 777);
		
		
		List<CharTriplet> triplets = new ArrayList<>();
		triplets.add(c);
		triplets.add(c2);
		
		serial.serializeToDisk(triplets);
		
		List<CharTriplet> newList = null;
		newList = serial.deserializeFromDisk(0);
		System.out.println(newList);
		System.out.println((int)newList.get(0).getValueOne());
		System.out.println((int)newList.get(0).getValueTwo());
		System.out.println((int)newList.get(0).getValueThree());
		System.out.println((int)newList.get(1).getValueOne());
		System.out.println((int)newList.get(1).getValueTwo());
		System.out.println((int)newList.get(1).getValueThree());
	}
	
}
